import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    property url soundUrl;
//    property var pcmDataM:new Array();
    property var pcmDatastr:AudioData.pcmData

    ListModel{
        id:dataModel
    }

    onPcmDatastrChanged: {
        try{
            if(dataModel.count===0)
                return;
            var jdata=JSON.parse(pcmDatastr).data;
            for(var a=0;a<jdata.length;a++){
                dataModel.setProperty(a,"pcmdata",jdata[a]);
            }

        }
        catch(e){
            console.log(e);
        }
    }

    id:mainwin
    Component.onCompleted: {
        for(var a=0;a<257;a++){
            dataModel.append({"pcmdata":0});
        }
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("文件")
            MenuItem {
                text: qsTr("&打开MP3文件")
                onTriggered: {
                    mdialog.open();
                }

            }
        }
    }


    FileDialog{
        id:mdialog
        folder: shortcuts.home
        nameFilters: [ "Sound files (*.mp3)" ]
        onAccepted:{
            soundUrl=mdialog.fileUrl;
            AudioData.setSource(soundUrl.toString().substring(8,soundUrl.toString().length));
            AudioData.playMusic();
        }
    }

    Row{
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        Repeater{
            model: dataModel
            delegate: Rectangle{
                width: mainwin.width/dataModel.count
                height: pcmdata*mainwin.height/255
                color:Qt.rgba(index/257,Math.abs(257-index)/257,Math.abs(257-index)/257,1)
                anchors.bottom: parent.bottom
//                radius: width/2
//                border.width:1
//                border.color:"#ffff12"
                Rectangle{
                    width:parent.width
                    height:1
                    color:"#454545"
                }

                Behavior on height{
                    PropertyAnimation{
                        properties: "height"
                        duration: 70
                    }
                }
            }
        }
    }

}
